<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
+ ---------------------------------------------------------------------- +
*/

function e107shop_payment_methods(){
    global $e107shop_db_name;
    $sql = new db;
    $payment_method = "<select name='payment' class='tbox'>";
    $sql->db_Select($e107shop_db_name['gateways'], "identifier", "active=1 AND (gateway_class IN (".USERCLASS_LIST."))");
    while($row = $sql->db_Fetch())
    {
        $gateway = $row['identifier'];
        // we need to add the php extention here because the file is readed in payment.php
        $payment_method .= "<option value='".$gateway.".php'>"
        .e107shop_translate_gateway($gateway)." </option>";
    }
    $payment_method .="</select>";
    return $payment_method;
}

function e107shop_translate_gateway($gwname) {
    $gwtrans = "lan_gateway_".$gwname;
    $result = constant($gwtrans);
    if (strlen($result) > 0) {
        return constant($gwtrans);
    } else {
        return $gwname." -> ".lan_gateway_missing;
    }
}

function e107shop_payment_forms($gateway, $total, $fname, $lname, $city, 
$state, $country, $region, $postage, $zip, $address, $email, $coupon){

    global $cart_id, $e107shop_db_name, $shop_settings, $tp;
    
    // security traps
    $gateway = $tp->toDB($gateway);
    $total   = $tp->toDB($total);
    $fname   = $tp->toDB($fname);
    $lname   = $tp->toDB($lname);
    $city    = $tp->toDB($city);
    $state   = $tp->toDB($state);
    $country = $tp->toDB($country);
    $region  = $tp->toDB($region);
    $postage = $tp->toDB($postage);
    $zip     = $tp->toDB($zip);
    $address = $tp->toDB($address);
    $email   = $tp->toDB($email);
    $coupon  = $tp->toDB($coupon);
    
    //
    // The websites currency
    //
    $currency = $shop_settings["currency"];

    //
    // Include the payment gateway php file - with the account info declared by the user
    //
    $payment_text = require("gateways/".$gateway);

    return $payment_text;
}

function e107shop_apply_coupon($total, $coupon_code){
    global $e107shop_db_name, $ns, $tp;
    $total       = $tp->toDB($total);
    $coupon_code = $tp->toDB($coupon_code);
    // Select Coupons from the database and set them to variables for use within this function
    $sql = new db;
    $sql->db_Select($e107shop_db_name['coupons'], "*", "coupon_code='".$coupon_code."'");
    if (!$row = $sql->db_Fetch()) {
        $ns->tablerender(lan_invalid_coupon_title, lan_coupon_not_found);
        return 0;
    }
    $coupon_expiry_date = $row["coupon_expiry_date"];
    $valid_for_x_uses = $row["xvalid_for_uses"];
    $coupon_used_x_times = $row["coupon_used_x_times"];
    $coupon_discount_amount = $row["coupon_discount_amount"];
    $coupon_discount_type = $row["coupon_discount_type"];
    // Check the coupons validity against the current date first
    $todays_date = mktime(0,0,0,date("m") ,date("d"), date("Y"));
    if($todays_date > $coupon_expiry_date || $coupon_expiry_date=="") {
        $ns->tablerender(lan_invalid_coupon_title, lan_invalid_coupon);
    } else {
        if($valid_for_x_uses > $coupon_used_x_times){
            // Check the type of the coupon
            // Type 1 = Amount
            // Type 2 = Percentage
            if($coupon_discount_type==1) {
                $total = $coupon_discount_amount;
            } else {
                // Calculate the new price with respect to the percentage discount
                $y = $coupon_discount_amount / 100;
                $total = $total * $y;
            }
            $total = number_format($total, 2, ".", ",");
            if (e_PAGE != "invoice.php") $ns->tablerender(lan_valid_coupon_title, "<center>".lan_valid_coupon."</center>");
            // Increase the coupon_used_x_times in the table
            $coupon_used_x_times += 1;
            $sql->db_Update($e107shop_db_name['coupons'], "coupon_used_x_times='".$coupon_used_x_times."' where coupon_code='".$coupon_code."'");
        } else {
            if (e_PAGE != "invoice.php") $ns->tablerender(lan_invalid_coupon_title, lan_max_used_coupon);
        }
    }
    return $total;
}
